package com.betterjr.modules.commission.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.betterjr.common.exception.BytterTradeException;
import com.betterjr.common.mapper.JsonMapper;
import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.BetterDateUtils;
import com.betterjr.common.utils.BetterStringUtils;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.MathExtend;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.common.utils.UserUtils;
import com.betterjr.mapper.pagehelper.Page;
import com.betterjr.modules.account.entity.CustOperatorInfo;
import com.betterjr.modules.commission.dao.CommissionRecordMapper;
import com.betterjr.modules.commission.data.CommissionConstantCollentions;
import com.betterjr.modules.commission.entity.CommissionBusPaymentCode;
import com.betterjr.modules.commission.entity.CommissionFile;
import com.betterjr.modules.commission.entity.CommissionFileDown;
import com.betterjr.modules.commission.entity.CommissionRecord;
import com.betterjr.modules.commission.entity.CommissionRecordAuditResult;
import com.betterjr.modules.config.dubbo.interfaces.IDomainAttributeService;
import com.betterjr.modules.customer.ICustMechBaseService;
import com.betterjr.modules.document.entity.CustFileItem;
import com.betterjr.modules.flie.service.FileDownService;
import com.betterjr.modules.flie.service.JxlsFileService;
import com.betterjr.modules.flie.service.TxtsFileService;

@Service
public class CommissionRecordService extends BaseService<CommissionRecordMapper, CommissionRecord> {

    @Autowired
    private CommissionFileService fileService;

    @Reference(interfaceClass = ICustMechBaseService.class)
    private ICustMechBaseService baseService;

    @Autowired
    private CommissionFileDownService CommissionfileDownService;

    @Autowired
    private FileDownService fileDownService;

    @Autowired
    CommissionPayBankResultService commissionService;

    @Reference(interfaceClass = IDomainAttributeService.class)
    private IDomainAttributeService domainAttributeDubboClientService;

    @Autowired
    private JxlsFileService jxlsFileService;

    @Autowired
    private TxtsFileService txtsFileService;

    /**
     * 通过佣金文件的fileId对佣金记录进行删除操作
     * 
     * @param anFileId
     * @return
     */
    public List<CommissionRecord> saveDeleteStatusByRefNo(Long anFileId) {

        BTAssert.notNull(anFileId, "删除佣金文件条件不符,");
        final Map<String, Object> queryMap = QueryTermBuilder.newInstance().put("fileId", anFileId).build();
        final List<CommissionRecord> recordList = this.selectByProperty(queryMap);
        for (final CommissionRecord record : recordList) {
            checkDeleteFileStatus(record, UserUtils.getOperatorInfo());
            record.setBusinStatus(CommissionConstantCollentions.COMMISSION_BUSIN_STATUS_DELETE);
            this.updateByPrimaryKeySelective(record);
        }
        return recordList;
    }

    /**
     * 通过佣金文件的id对佣金记录进行作废操作
     * 
     * @param anId
     *            佣金文件id
     */
    public List<CommissionRecord> saveCannulStatusByFileId(Long anFileId) {

        BTAssert.notNull(anFileId, "删除佣金文件条件不符,");
        final Map<String, Object> queryMap = QueryTermBuilder.newInstance().put("fileId", anFileId).build();
        final List<CommissionRecord> recordList = this.selectByProperty(queryMap);
        for (final CommissionRecord record : recordList) {
            checkCannulFileStatus(record, UserUtils.getOperatorInfo());
            record.setBusinStatus(CommissionConstantCollentions.COMMISSION_BUSIN_STATUS_DELETE);
            this.updateByPrimaryKeySelective(record);
        }
        return recordList;

    }

    /**
     * 检查是否符合作废的条件
     * 
     * @param anFile
     * @param anOperatorInfo
     */
    private void checkCannulFileStatus(CommissionRecord anRecord, CustOperatorInfo anOperatorInfo) {

        BTAssert.notNull(anOperatorInfo, "登录已过期，请重新登录");
        checkStatus(anRecord.getPayStatus(), CommissionConstantCollentions.COMMISSION_PAY_STATUS_FAILURE, true,
                "当前文件记录已经开始付款，不能删除");
        checkStatus(anRecord.getPayStatus(), CommissionConstantCollentions.COMMISSION_PAY_STATUS_SUCCESS, true,
                "当前文件记录已经开始付款，不能删除");
        checkStatus(anRecord.getBusinStatus(), CommissionConstantCollentions.COMMISSION_RECORD_BUSIN_STATUS_PAY, true,
                "当前文件记录进入付款流程中，不能删除");
        checkStatus(anRecord.getBusinStatus(), CommissionConstantCollentions.COMMISSION_RECORD_BUSIN_STATUS_DELETE,
                true, "当前记录已经作废，请不要重复作废");
        checkStatus(anRecord.getOperOrg(), anOperatorInfo.getOperOrg(), false, "你没有当前文件的删除权限");

    }

    /**
     * 检查是否符合删除的条件
     * 
     * @param anFile
     * @param anOperatorInfo
     */
    private void checkDeleteFileStatus(CommissionRecord anRecord, CustOperatorInfo anOperatorInfo) {

        BTAssert.notNull(anOperatorInfo, "登录已过期，请重新登录");
        checkStatus(anRecord.getPayStatus(), CommissionConstantCollentions.COMMISSION_PAY_STATUS_FAILURE, true,
                "当前文件记录已经开始付款，不能删除");
        checkStatus(anRecord.getPayStatus(), CommissionConstantCollentions.COMMISSION_PAY_STATUS_SUCCESS, true,
                "当前文件记录已经开始付款，不能删除");
        checkStatus(anRecord.getBusinStatus(), CommissionConstantCollentions.COMMISSION_RECORD_BUSIN_STATUS_PAY, true,
                "当前文件记录进入付款流程中，不能删除");
        checkStatus(anRecord.getConfirmStatus(), CommissionConstantCollentions.COMMISSION_FILE_CONFIRM_STATUS_EFFECTIVE,
                true, "当前文件记录已经确认通过，不能删除");
        checkStatus(anRecord.getConfirmStatus(),
                CommissionConstantCollentions.COMMISSION_FILE_CONFIRM_STATUS_INEFFECTIVE, true, "当前文件记录已经确认，不能删除");
        checkStatus(anRecord.getOperOrg(), anOperatorInfo.getOperOrg(), false, "你没有当前文件的删除权限");

    }

    /**
     * 检查状态信息
     */
    public void checkStatus(String anBusinStatus, String anTargetStatus, boolean anFlag, String anMessage) {
        if (BetterStringUtils.equals(anBusinStatus, anTargetStatus) == anFlag) {
            logger.warn(anMessage);
            throw new BytterTradeException(40001, anMessage);
        }
    }

    /**
     * 文件解析上传
     * 
     * @param anListData
     * @return
     */
    public List<CommissionRecord> saveRecordListWithMap(List<Map<String, Object>> anListData) {

        BTAssert.notNull(anListData, "当前解析文件没有数据");
        final List<CommissionRecord> records = new ArrayList<>();
        int i = CommissionConstantCollentions.COMMISSION_FILE_BEGIN_ROW;
        try {
            for (final Map<String, Object> dataMap : anListData) {
                i++;
                final CommissionRecord record = new CommissionRecord();
                record.initResolveValue(dataMap, i, UserUtils.getOperatorInfo());
                // 根据银行全称匹配银行直通车代码
                final CommissionBusPaymentCode findCondition = new CommissionBusPaymentCode();
                findCondition.setBankFullName(record.getBankName());
                final CommissionBusPaymentCode findBankPayCode = commissionService.selectOne(findCondition);
                if (findBankPayCode != null) {
                    record.setBankPayCode(findBankPayCode.getPaymentNo());
                } else {

                }
                // record.getAmount().multiply(record.getUnit())
                if (record.getBalance().compareTo(record.getUnit().multiply(new BigDecimal(record.getAmount()))) != 0) {
                    BTAssert.notNull(null, "第" + i + "行的数量为" + record.getAmount() + "  单价为：" + record.getUnit()
                            + " 总金额为： " + record.getBalance() + " ；数量*单价!=总金额");
                }
                this.insert(record);
                records.add(record);
            }

        }
        catch (final Exception e) {
            BTAssert.notNull(null, e.getMessage());
        }

        return records;
    }

    /**
     * 佣金记录查询
     * 
     * @param anMap
     *            查询条件
     * @param anFlag
     *            1：查询总的条数 2 不查询总的条数
     * @param anPageNum
     *            当前页数
     * @param anPageSize
     *            每页显示的总的数量
     * @return
     */
    public Page queryRecordList(Map<String, Object> anMap, String anFlag, int anPageNum, int anPageSize) {

        BTAssert.notNull(anMap, "查询佣金记录条件为空");
        // 去除空白字符串的查询条件
        anMap = Collections3.filterMapEmptyObject(anMap);
        // 查询当前公司的佣金文件
        anMap.put("operOrg", UserUtils.getOperatorInfo().getOperOrg());
        anMap.put("NEbusinStatus", CommissionConstantCollentions.COMMISSION_RECORD_BUSIN_STATUS_DELETE);

        final Page<CommissionRecord> recordList = this.selectPropertyByPage(anMap, anPageNum, anPageSize,
                "1".equals(anFlag), "id desc");

        return recordList;
    }

    /**
     * 查询佣金文件审核全部查询佣金记录所有信息
     * 
     * @param anMap
     * @param anFlag
     * @param anPageNum
     * @param anPageSize
     * @return
     */
    public Page<CommissionRecord> queryCanAuditRecordList(Map<String, Object> anMap, String anFlag, int anPageNum,
            int anPageSize) {

        BTAssert.notNull(anMap, "查询佣金记录条件为空");
        // 去除空白字符串的查询条件
        anMap = Collections3.filterMapEmptyObject(anMap);
        // 查询当前公司的佣金文件
        anMap.put("operOrg", UserUtils.getOperatorInfo().getOperOrg());
        anMap.put("businStatus", CommissionConstantCollentions.COMMISSION_BUSIN_STATUS_NO_HANDLE);
        anMap.put("payStatus", CommissionConstantCollentions.COMMISSION_PAY_STATUS_NO_HANDLE);
        anMap.put("confirmStatus", CommissionConstantCollentions.COMMISSION_FILE_CONFIRM_STATUS_UNCONFIRMED);

        final Page<CommissionRecord> recordList = this.selectPropertyByPage(anMap, anPageNum, anPageSize,
                "1".equals(anFlag), "id desc");

        return recordList;
    }

    /**
     * 查询佣金记录详情
     * 
     * @param anRefNo
     * @return
     */
    public CommissionRecord findRecord(String anRefNo) {
        BTAssert.notNull(anRefNo, "查询佣金记录条件为空");
        final CommissionRecord record = this.selectOne(new CommissionRecord(anRefNo));
        BTAssert.notNull(record, "查询佣金记录为空");
        return record;
    }

    /**
     * 
     * @Title: findRecord 
     * @Description: TODO(查找原始的佣金记录有没有存在) 
     * @param @param anRefNo
     * @param @return 参数说明 
     * @return CommissionRecord 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月21日 下午6:34:40
     */
    public CommissionRecord findOriginRecord(String anRefNo) {
        BTAssert.notNull(anRefNo, "查询佣金记录条件为空");
        final CommissionRecord record = this.selectOne(new CommissionRecord(anRefNo));

        return record;

    }

    /**
     * 审核佣金记录全部
     * 
     * @param anMap
     * @return
     */
    public CommissionRecordAuditResult saveAuditRecordList(Map<String, Object> anMap) {

        BTAssert.notNull(anMap, "审核佣金记录条件为空");
        BTAssert.notNull(anMap.get("importDate"), "审核佣金记录条件为空");
        BTAssert.notNull(anMap.get("custNo"), "审核佣金记录条件为空");
        final CustOperatorInfo operatorInfo = UserUtils.getOperatorInfo();
        BTAssert.notNull(operatorInfo, "登录状态失效，请重新登录");
        logger.info("佣金记录批量审核 begin：  saveAuditRecordList    审核人为: " + operatorInfo.getName());
        final String operOrg = baseService.findBaseInfo(Long.parseLong(anMap.get("custNo").toString())).getOperOrg();
        checkStatus(operOrg, operatorInfo.getOperOrg(), false, "你没有当前公司审核记录权限");
        // 去除空白字符串的查询条件
        anMap = Collections3.filterMapEmptyObject(anMap);
        // 查询未解析的文件(未解析文件 和解析失败未删除文件)
        final List<CommissionFile> fileList = fileService.queryFileNoResolve(anMap);
        logger.info("佣金记录批量审核 ：  saveAuditRecordList  查询到未解析的文件 个数为：" + fileList.size());
        if (!Collections3.isEmpty(fileList)) {
            return CommissionRecordAuditResult.fail(fileList.size());
        }
        // 查询到所有的到custNO 和导入日期并且是未审核的所有佣金记录
        anMap.put("payStatus", CommissionConstantCollentions.COMMISSION_PAY_STATUS_NO_HANDLE);
        anMap.put("businStatus", CommissionConstantCollentions.COMMISSION_BUSIN_STATUS_NO_HANDLE);
        anMap.put("confirmStatus", CommissionConstantCollentions.COMMISSION_FILE_CONFIRM_STATUS_UNCONFIRMED);
        final List<CommissionRecord> recordList = this.selectByClassProperty(CommissionRecord.class, anMap);
        if (Collections3.isEmpty(recordList)) {
            return CommissionRecordAuditResult.fail("当前没有记录可供审核，当前记录已经审核完毕");
        }
        int recordAmount = 0;
        BigDecimal blance = new BigDecimal(0);
        final Set<Long> fileSet = new HashSet<>();
        // 1 修改每条佣金记录的状态
        for (final CommissionRecord commissionRecord : recordList) {
            recordAmount++;
            blance = MathExtend.add(blance, commissionRecord.getBalance());
            fileSet.add(commissionRecord.getFileId());
            commissionRecord.saveAuditInit(operatorInfo);
            this.updateByPrimaryKeySelective(commissionRecord);
        }
        logger.info("佣金记录批量审核 ：  saveAuditRecordList  查询到未审核的记录 个数为：" + recordAmount + "  :总的金额为：" + blance
                + "   审核的文件数量为=" + fileSet.size());

        // 3 生成佣金记录文件
        final CommissionFileDown fileDownRecord = saveAddFileDownRecord(recordList, recordAmount, blance);

        // 回写佣金记录下载文件id到佣金记录中
        for (final CommissionRecord commissionRecord : recordList) {
            final CommissionRecord record = new CommissionRecord();
            record.setId(commissionRecord.getId());
            record.setFileDownId(fileDownRecord.getId());
            this.updateByPrimaryKeySelective(record);
        }

        // 2审核文件列表
        fileService.saveAuditFile(fileSet, fileDownRecord.getId());

        return CommissionRecordAuditResult.ok(fileSet.size(), recordAmount, blance);

    }

    /**
     * 
     * @Title: saveAuditRecordEditBankPayCode 
     * @Description: TODO(修改和添加佣金记录为匹配恰当的直通车代码) 
     * @param @param anMap
     * @param @return 参数说明 
     * @return CommissionRecord 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年10月28日 下午4:01:29
     */
    public CommissionRecord saveAuditRecordEditBankPayCode(CommissionRecord anBankPayCodeRecord) {
        BTAssert.notNull(anBankPayCodeRecord, "银行直通车代码条件为为空");
        BTAssert.notNull(anBankPayCodeRecord.getFileDownId(), "审核佣金记录条件为空");
        BTAssert.notNull(anBankPayCodeRecord.getBankPayCode(), "银行直通车不能为空");
        // 判断直通车代码只能是数字
        final String regexNum = "\\d+";
        BTAssert.isTrue(anBankPayCodeRecord.getBankPayCode().matches(regexNum), "银行直通车代码只能是数字，请重新输入！");
        final CustOperatorInfo operatorInfo = UserUtils.getOperatorInfo();
        BTAssert.notNull(operatorInfo, "登录状态失效，请重新登录");
        logger.info("佣金记录银行直通车代码添加人为: " + operatorInfo.getName());
        // 根据id查询到要修改的直通车代码记录
        final CommissionRecord record = this.selectByPrimaryKey(anBankPayCodeRecord.getId());
        // 判断有没有要修改的数据存在
        BTAssert.notNull(record, "没有这条记录存在");
        record.saveAuditInit(operatorInfo);
        record.setBankPayCode(anBankPayCodeRecord.getBankPayCode());
        this.updateByPrimaryKeySelective(record);
        // 每次编辑时做出直通车代码状态的改变
        final List<CommissionRecord> recordList = this.queryRecordListByDownFileId(anBankPayCodeRecord.getFileDownId());
        BTAssert.notNull(recordList, "查询佣金记录为空");
        // 得到匹配的状态
        final Long recordStatus = getBankPayCodeStatus(recordList);

        // 得到下载文件的记录数据
        final CommissionFileDown fileDown = new CommissionFileDown();
        fileDown.setId(anBankPayCodeRecord.getFileDownId());

        // 查询判断有没有这条记录存在
        final CommissionFileDown fileDownRecord = CommissionfileDownService.selectOne(fileDown);
        BTAssert.notNull(fileDownRecord, "佣金下载文件查询为空");
        fileDownRecord.setBankPayCodeStatus(recordStatus);

        // 更新佣金下载文件的直通车代码状态
        CommissionfileDownService.updateByPrimaryKey(fileDownRecord);

        // 生成修改后的佣金付款文件
        final Map<String, Object> fileDownId = new HashMap<String, Object>();
        fileDownId.put("fileDownId", fileDownRecord.getId());
        final List<CommissionRecord> findRecordList = this.selectByProperty(fileDownId);
        saveUpdateFileDownRecord(findRecordList, fileDownRecord.getId());
        return record;
    }

    /**
     * 
     * @Title: saveUpdateFileDownRecord 
     * @Description: TODO(更新佣金下载文件) 
     * @param @param recordList
     * @param @param id 参数说明 
     * @return void 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月3日 下午2:19:46
     */
    private void saveUpdateFileDownRecord(List<CommissionRecord> recordList, Long id) {
        BTAssert.notNull(recordList, "更新佣金记录条件为空");
        logger.info("佣金记录recordList: " + recordList + "操作员：" + UserUtils.getOperatorInfo().getName());
        final CommissionFileDown fileDown = new CommissionFileDown();
        fileDown.setId(id);
        // 保存存在直通代码的记录
        final List<CommissionRecord> existPayCodeRecordList = new ArrayList<CommissionRecord>();
        // 保存不存在直通车代码的记录
        final List<CommissionRecord> noPayCodeRecordList = new ArrayList<CommissionRecord>();

        for (final CommissionRecord record : recordList) {
            if (record.getBankPayCode() != null) {
                existPayCodeRecordList.add(record);
            } else {
                noPayCodeRecordList.add(record);
            }
        }
        // 得到直通车匹配状态
        final Long recordStatus = getBankPayCodeStatus(recordList);
        fileDown.setBankPayCodeStatus(recordStatus);
        // 得到佣金支付未含直通车代码文件的id
        Long commissionPayExcelFileId = null;
        // 得到佣金支付含直通车代码文件的id
        Long commissionPayTxtFileId = null;
        if (noPayCodeRecordList.size() != 0) {
            commissionPayExcelFileId = SaveExcelFile(noPayCodeRecordList);
            fileDown.setPayExcelFileId(commissionPayExcelFileId);
        } else {
            commissionPayExcelFileId = 0l;
            fileDown.setPayExcelFileId(commissionPayExcelFileId);
        }

        if (existPayCodeRecordList.size() != 0) {
            commissionPayTxtFileId = SaveTxtFile(existPayCodeRecordList);
            fileDown.setPayTxtFileId(commissionPayTxtFileId);
        } else {
            commissionPayTxtFileId = 0l;
            fileDown.setPayTxtFileId(commissionPayTxtFileId);
        }
        logger.info("更新下载表内容fileDown: " + fileDown + "操作员：" + UserUtils.getOperatorInfo().getName());
        CommissionfileDownService.updateByPrimaryKeySelective(fileDown);
    }

    /**
     * 生成佣金记录下载文件
     * 
     * @param recordList
     * @param recordAmount
     * @param blance
     */
    private CommissionFileDown saveAddFileDownRecord(List<CommissionRecord> recordList, int recordAmount,
            BigDecimal blance) {

        final Map<String, Object> data = new HashMap<>();
        data.put("recordList", recordList);
        data.put("companyName", "   ");
        final String fileName = BetterDateUtils.getNumDate() + recordList.get(0).getCustName() + "打款明细表.xlsx";
        // 得到文件id
        final Long fileId = getExportFileId();

        if (fileId == null) {
            BTAssert.notNull(null, "请先联系平台上传佣金导出模版");
        }
        final CustFileItem fileItem = fileDownService.uploadCommissionRecordFileis(data, fileId, fileName);
        final CommissionFileDown fileDown = new CommissionFileDown();
        fileDown.saveAddInit(recordAmount, blance, recordList.get(0));
        fileDown.setBatchNo(fileItem.getBatchNo());
        fileDown.setFileId(fileItem.getId());
        fileDown.setCpsRecordFileId(recordList.get(0).getFileId());
        // 保存存在直通代码的记录
        final List<CommissionRecord> existPayCodeRecordList = new ArrayList<CommissionRecord>();
        // 保存不存在直通车代码的记录
        final List<CommissionRecord> noPayCodeRecordList = new ArrayList<CommissionRecord>();

        for (final CommissionRecord record : recordList) {
            if (record.getBankPayCode() != null) {
                existPayCodeRecordList.add(record);
            } else {
                noPayCodeRecordList.add(record);
            }
        }
        // 得到直通车匹配状态
        final Long recordStatus = getBankPayCodeStatus(recordList);
        // 得到佣金支付未含直通车代码文件的id
        final Long commissionPayExcelFileId = SaveExcelFile(noPayCodeRecordList);
        // 得到佣金支付含直通车代码文件的id
        final Long commissionPayTxtFileId = SaveTxtFile(existPayCodeRecordList);
        fileDown.setBankPayCodeStatus(recordStatus);
        fileDown.setPayExcelFileId(commissionPayExcelFileId);
        fileDown.setPayTxtFileId(commissionPayTxtFileId);
        // 初始化下载次数为0
        fileDown.setFileDownAcount(0l);
        CommissionfileDownService.insert(fileDown);
        return fileDown;
    }

    /**
     * 
     * @Title: SaveExcelFile 
     * @Description: TODO(根据银行直通车代码没匹配成功的数据生成excel文档) 
     * @param @param anMap 参数说明 
     * @return void 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月1日 上午11:23:59
     */
    public Long SaveExcelFile(List<CommissionRecord> noPayCodeRecordList) {
        BTAssert.notNull(noPayCodeRecordList, "存入佣金记录条件为空");
        logger.info("recordList数据为：" + noPayCodeRecordList + " 操作用户为：" + UserUtils.getOperatorInfo().getName());
        // 根据文件模版生成excel文件
        final String directCommissionPayExportExcelTemplate = domainAttributeDubboClientService
                .findString("GLOBAL_DIRECT_PAY_EXPORT_EXCEL_TEMPLATE");
        logger.info("directCommissionPayExportExcelTemplate:" + directCommissionPayExportExcelTemplate + " 操作用户为："
                + UserUtils.getOperatorInfo().getName());
        Long fileId = 0l;
        String fileType = "";
        if (directCommissionPayExportExcelTemplate != null) {
            final Map<String, Object> templateMp = JsonMapper.parserJson(directCommissionPayExportExcelTemplate);
            fileId = Long.parseLong(templateMp.get("id").toString());
            fileType = (String) templateMp.get("fileType");
            if (BetterStringUtils.isBlank(fileType)) {
                fileType = ".xlsx";
            }
        }

        final String fileName = "企业佣金付款表(不含直通车代码)" + BetterDateUtils.getDate() + fileType;
        // 生成文件
        final Map<String, Object> fileMap = new HashMap<String, Object>();
        fileMap.put("noPayCodeRecordList", noPayCodeRecordList);
        try {
            final CustFileItem custFile = jxlsFileService.transformXLSFileToList(fileMap, fileId, fileName);
            logger.info("生成后的文件，custFile:" + custFile);
            return custFile.getId();
        }
        catch (final Exception e) {
            logger.error("异常：", e);
        }
        return null;
    }

    /**
     * 
     * @Title: SaveTxtFile 
     * @Description: TODO(根据匹配成功的银行直通车代码生成txt文件并得到文件id) 
     * @param @param recordList
     * @param @return 参数说明 
     * @return Long 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月2日 上午10:42:27
     */
    public Long SaveTxtFile(List<CommissionRecord> recordList) {
        BTAssert.notNull(recordList, "存入佣金记录条件为空");
        logger.info("recordList数据为：" + recordList + " 操作用户为：" + UserUtils.getOperatorInfo().getName());
        final String fileName = "企业直通车付款表" + BetterDateUtils.getDate() + ".txt";
        // 生成文件
        try {
            final CustFileItem custFile = txtsFileService.transformXLSFileToTxt(recordList, fileName);
            logger.info("生成后的文件，custFile:" + custFile);
            return custFile.getId();
        }
        catch (final Exception e) {
            logger.error("异常：", e);
        }
        return null;
    }

    // 判断银行直通车代码匹配程度
    private Long getBankPayCodeStatus(List<CommissionRecord> recordList) {
        Long recordStatus = 0l;
        int i = 0;
        for (final CommissionRecord record : recordList) {
            if (record.getBankPayCode() != null) {
                recordStatus = 1l;
                i++;
            }
            if (i == recordList.size()) {
                recordStatus = 2l;
            }
        }
        return recordStatus;
    }

    // 得要佣金文件ID
    private Long getExportFileId() {

        final String exportCommissionTemplate = domainAttributeDubboClientService
                .findString("GLOBAL_COMMISSION_EXPORT_TEMPLATE");

        if (StringUtils.isNoneBlank(exportCommissionTemplate)) {

            final Map<String, Object> templateMp = JsonMapper.parserJson(exportCommissionTemplate);
            final Long fileId = Long.parseLong(templateMp.get("id").toString());

            return fileId;

        } else {

            BTAssert.notNull(null, "请先联系平台上传佣金导出模版");

        }

        return null;

    }

    /**
     * 审核单条记录信息
     * 
     * @param anRefNo
     * @return
     */
    public CommissionRecord saveAudtiRecord(String anRefNo) {

        BTAssert.notNull(anRefNo, "审核佣金记录条件为空");

        final CommissionRecord record = this.selectOne(new CommissionRecord(anRefNo));

        BTAssert.notNull(record, "审核佣金记录为空");
        // 校验当前记录的状态
        checkAuditRecordStatus(record, UserUtils.getOperatorInfo());
        record.saveAuditInit(UserUtils.getOperatorInfo());
        this.updateByPrimaryKeySelective(record);
        // 尝试审核佣金文件
        attmeptAuditCommissionFile(record);

        return record;

    }

    /**
     * 当前记录的文件id 导入时间 拥有机构 没有未处理的单据则审核对应的佣金文件
     * 
     * @param anRecord
     */
    private void attmeptAuditCommissionFile(CommissionRecord anRecord) {

        final Map<String, Object> queryParam = QueryTermBuilder.newInstance().put("fileId", anRecord.getFileId())
                .put("importDate", anRecord.getImportDate()).put("operOrg", anRecord.getOperOrg())
                .put("NEid", anRecord.getId())
                .put("businStatus", CommissionConstantCollentions.COMMISSION_BUSIN_STATUS_NO_HANDLE).build();

        final List<CommissionRecord> recordList = this.selectByClassProperty(CommissionRecord.class, queryParam);

        if (Collections3.isEmpty(recordList)) {
            fileService.saveAuditFile(anRecord.getFileId());
        }

    }

    /**
     * 校验当前佣金记录是否符合审核条件
     * 
     * @param anRecord
     * @param anOperatorInfo
     *            //业务状态 0 未处理 1 已审核(最终可供 拜特支付状态) 2已支付 3删除
     */
    private void checkAuditRecordStatus(CommissionRecord anRecord, CustOperatorInfo anOperatorInfo) {

        BTAssert.notNull(anOperatorInfo, "登录已过期，请重新登录");
        checkStatus(anRecord.getPayStatus(), CommissionConstantCollentions.COMMISSION_PAY_STATUS_FAILURE, true,
                "当前记录已经付款，不能重新审核");
        checkStatus(anRecord.getPayStatus(), CommissionConstantCollentions.COMMISSION_PAY_STATUS_SUCCESS, true,
                "当前记录已经付款，不能重新审核");
        checkStatus(anRecord.getBusinStatus(), CommissionConstantCollentions.COMMISSION_RECORD_BUSIN_STATUS_PAY, true,
                "当前记录已经付款，不能重新审核");
        checkStatus(anRecord.getBusinStatus(), CommissionConstantCollentions.COMMISSION_RECORD_BUSIN_STATUS_DELETE,
                true, "当前记录已经删除，不能重新审核");
        checkStatus(anRecord.getOperOrg(), anOperatorInfo.getOperOrg(), false, "你没有当前文件的审核权限");

    }

    public List<CommissionRecord> queryRecordListByFileId(Long anFileId) {

        final Map<String, Object> map = QueryTermBuilder.newInstance().put("fileId", anFileId).build();
        return this.selectByProperty(map, "id Desc");
    }

    public List<CommissionRecord> queryRecordListByDownFileId(Long anFileDownId) {

        final Map<String, Object> map = QueryTermBuilder.newInstance().put("fileDownId", anFileDownId).build();
        return this.selectByProperty(map, "id Desc");
    }

    /**
     * @param anCustNo
     * @param anImportDate
     * @return
     */
    public Page<CommissionRecord> queryRecordListByImportDate(final Long anCustNo, final String anImportDate,
            final int anFlag, final int anPageNum, final int anPageSize) {
        final Map<String, Object> map = QueryTermBuilder.newInstance().put("custNo", anCustNo)
                .put("importDate", anImportDate).put("businStatus", "1")
                .put("confirmStatus", CommissionConstantCollentions.COMMISSION_FILE_CONFIRM_STATUS_EFFECTIVE).build();
        return this.selectPropertyByPage(map, anPageNum, anPageSize, anFlag == 1);
    }

    public Map<String, Object> findRecordListCount(final Long anCustNo, final String anImportDate) {
        return this.mapper.countRecordList(anCustNo, anImportDate);
    }

    /**
     * 通过文件ids分页查询
     * 
     * @param anIds
     * @param anFlag
     * @param anPageNum
     * @param anPageSize
     * @return
     */
    public Page<CommissionRecord> queryRecordPageByFileIds(List<Long> anIds, String anFlag, int anPageNum,
            int anPageSize) {

        final Map<String, Object> map = QueryTermBuilder.newInstance().put("fileId", anIds).build();
        return this.selectPropertyByPage(map, anPageNum, anPageSize, "1".equals(anFlag), "id desc");
    }

    /**
     * 平台审核导出文件记录信息
     * 
     * @param anFile
     */
    public List<CommissionRecord> saveUpdateByCommissionFile(CommissionFile anFile) {

        BTAssert.notNull(anFile, "审核导出文件不存在！");
        BTAssert.notNull(anFile.getId(), "审核导出文件为空");
        final List<CommissionRecord> recordList = queryRecordListByFileId(anFile.getId());
        BTAssert.notNull(recordList, "审核导出文件为空");
        for (final CommissionRecord record : recordList) {

            checkOperatorAuditRecordStatus(record);
            record.setConfirmStatus(anFile.getConfirmStatus());
            this.updateByPrimaryKeySelective(record);
        }

        return recordList;
    }

    private void checkOperatorAuditRecordStatus(CommissionRecord anRecord) {

        checkStatus(anRecord.getConfirmStatus(), CommissionConstantCollentions.COMMISSION_FILE_CONFIRM_STATUS_EFFECTIVE,
                true, "当前文件记录已经已经审核通过，不能重复审核");
        checkStatus(anRecord.getConfirmStatus(),
                CommissionConstantCollentions.COMMISSION_FILE_CONFIRM_STATUS_INEFFECTIVE, true, "当前文件记录已经审核未通过，不能重复审核");
        checkStatus(anRecord.getPayStatus(), CommissionConstantCollentions.COMMISSION_PAY_STATUS_FAILURE, true,
                "当前文件记录已经付款，不能审核");
        checkStatus(anRecord.getPayStatus(), CommissionConstantCollentions.COMMISSION_PAY_STATUS_SUCCESS, true,
                "当前文件记录已经付款，不能审核");
        checkStatus(anRecord.getBusinStatus(), CommissionConstantCollentions.COMMISSION_RECORD_BUSIN_STATUS_DELETE,
                true, "当前文件记录已经删除，不能审核");
        checkStatus(anRecord.getBusinStatus(), CommissionConstantCollentions.COMMISSION_RECORD_BUSIN_STATUS_PAY, true,
                "当前文件记录进入付款流程，不能审核");
        checkStatus(anRecord.getBusinStatus(), CommissionConstantCollentions.COMMISSION_BUSIN_STATUS_NO_HANDLE, true,
                "当前文件记录还未达到审核条件，不能审核");

    }
}
